package com.teziyou.commodity.logic;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jodd.bean.BeanUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mytravel.commodityBridge.service.bean.OnSaleTicket;
import com.mytravel.commodityBridge.service.bean.TicketPaymentWayUse;
import com.mytravel.commodityBridge.service.bean.TicketPromotionPolicyUse;
import com.teziyou.commodity.bean.Page;
import com.teziyou.commodity.service.SceneService;
import com.teziyou.commodity.service.TicketService;
import com.teziyou.commodity.util.DateUtil;
import com.teziyou.commodity.util.Lang;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class ShelfLogic {
	
	@Autowired
	private TicketService ticketService;
	@Autowired
	private SceneService sceneService;
	
	public Map loadTicket(long ticketId) {
		Map ticket = null;//ticketService.findTicketById(ticketId);
		long sceneId = Lang.toInt(ticket.get("scenic_id"));
		Map scene = sceneService.findSceneById(sceneId);
		ticket.put("scene_name", scene.get("name"));
		//ticket.put("imgs", ticketService.findImgsByTicketId(ticketId));
		return ticket;
	}
	
	public Page getRelateTickets(int pageNo, long ticketId) {
		List tickets = ticketService.getRelatedTicketsByTicketId(ticketId);
		Iterator iter = tickets.iterator();
		while(iter.hasNext()) {
			Map ticket = (Map) iter.next();
			System.err.println(ticket);
			long sceneId = Lang.toLong(ticket.get("scenic_id"));
			Map scene = sceneService.findSceneById(sceneId);
			ticket.put("scene", scene);
			ticket.put("from_time_display", DateUtil.formatDate(new Date(Lang.toLong(ticket.get("from_time"))), "yyyy.MM.dd"));
			ticket.put("end_time_display", DateUtil.formatDate(new Date(Lang.toLong(ticket.get("end_time"))), "yyyy.MM.dd"));
			
		}
		int count = ticketService.getRelatedTicketsCountByTicketId(ticketId);
		Page ticketPage = new Page(pageNo, count, tickets); 
		return ticketPage;
	}
	
	public void saveShelfTicket(Map paramMap) {
		String policyStr = Lang.toString(paramMap.get("ticketPromotionPolicyUse"));
		List policys = JSON.parseArray(policyStr);
		System.err.println(policys);
		Iterator iter = policys.iterator();
		List<TicketPromotionPolicyUse> tps = new ArrayList<>();
		while(iter.hasNext()) {
			Map policy = (Map) iter.next();
			TicketPromotionPolicyUse tp = new TicketPromotionPolicyUse();
			tp.setDictionaryid(Lang.toInt(policy.get("dictionaryid")));
			tp.setPromotionBaseData(Lang.toString(policy.get("promotionBaseData")));
			tp.setPromotionData(Lang.toString(policy.get("promotionData")));
			tps.add(tp);
		}
		paramMap.remove("ticketPromotionPolicyUse");
		String paywayStr = Lang.toString(paramMap.get("payway"));
		String[] payways =  paywayStr.split(",");
		List<TicketPaymentWayUse> tus = new ArrayList<>();
		for(String id:payways) {
			TicketPaymentWayUse tu = new TicketPaymentWayUse();
			System.err.println(id);
			tu.setDictionaryId(Integer.parseInt(id));
			tus.add(tu);
		}
		paramMap.remove("payway");
		OnSaleTicket ost = new OnSaleTicket();
		BeanUtil.populateBean(ost, paramMap);
		ost.setTicketPromotionPolicyUse(tps);
		ost.setPaymentWayUseList(tus);
		ost.setIsok(1);
		System.out.println(ost);
		
		ticketService.addOnSaleTicketInfo(ost);
	}

	public void updateShelfStatus(String id, int isok) {
		ticketService.updateOnSaleIsOK(id, isok==1?0:1);
	}

}
